//
//  ByViewMineHeader.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/10/20.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByViewMineHeader.h"

@implementation ByViewMineHeader
{
    UIImageView *_bgView;
    UIImageView *_iconView;
    UILabel     *_nickLabel;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor redColor];
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    [self addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    
    // 背景视图
    _bgView = [[UIImageView alloc] init];
    _bgView.image = [UIImage imageNamed:@"头视图背景"];
    [self addSubview:_bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).priorityMedium();
    }];
    
    // 昵称
    _nickLabel = [[UILabel alloc] init];
    _nickLabel.adjustsFontSizeToFitWidth = true;
    _nickLabel.numberOfLines = 1;
    _nickLabel.text = @"蜜蜂001";
    _nickLabel.font = FONT(16);
    _nickLabel.textAlignment = NSTextAlignmentCenter;
    _nickLabel.textColor = BY_GOLD_COLOR;
    [self addSubview:_nickLabel];
    [_nickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.bottom.equalTo(self).offset(-10);
        make.height.equalTo(@20);
    }];
    
    // 头像
    _iconView = [[UIImageView alloc] init];
    _iconView.layer.cornerRadius = 30;
    _iconView.layer.masksToBounds = true;
    _iconView.layer.borderColor = BY_GOLD_COLOR.CGColor;
    _iconView.layer.borderWidth = 1;
    _iconView.image = [UIImage imageWithColor:[UIColor whiteColor]];
    [self addSubview:_iconView];
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(60, 60));
        make.centerX.equalTo(self);
        make.bottom.equalTo(_nickLabel.mas_top).offset(-10);
    }];
}
- (void)setIsLogin:(BOOL)isLogin {
    
    _isLogin = isLogin;
    self.userInteractionEnabled = !isLogin;
    // 实际数据要去userModel中取值
    _iconView.image = isLogin ? [UIImage imageWithColor:[UIColor whiteColor]] : [UIImage imageNamed:@"默认头像"];
    _nickLabel.text = isLogin ? USER.nickname : @"登录";
}

- (void)setOffset:(CGFloat)offset {
    
    _offset = offset;
    [self setNeedsUpdateConstraints];
}
- (void)updateConstraints {
    
    [_bgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(self.offset);
    }];
    
    [super updateConstraints];
}

- (void)login {
    
    ByLoginVC *vc = [ByLoginVC new];
    ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
    [self.viewController presentViewController:navi animated:true completion:nil];

}
@end
